package com.aaa.controller;

import com.aaa.service.ExamService;
import com.aaa.service.ExamServiceImpl;
import com.aaa.service.ScoreServiceImpl;
import com.aaa.service.ScroceService;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet(name = "ScoreServlet",urlPatterns = "/score.action")
public class ScoreServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("method");
        if (method!=null) {
            switch (method) {
                case "queryClass":
                    queryClass(request, response);
                    break;
                case "queryScore":
                    queryScore(request, response);
                    break;

        }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      doPost(request,response);
    }

    protected void queryClass(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

        ExamService service=new ExamServiceImpl();
        List list= service.queryClass();
        String str= JSON.toJSONStringWithDateFormat(list,"yyyy-MM-dd");
        response.getWriter().print(str);

    }

    protected void queryScore(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
      String classid=request.getParameter("classid");
      int class_id= Integer.parseInt(classid);
      String examid=request.getParameter("exam_id");
      int exam_id= Integer.parseInt(examid);
      ScroceService scroceService=new ScoreServiceImpl();
      List<Map> list=scroceService.queryScore(class_id,exam_id);
      String str=JSON.toJSONStringWithDateFormat(list,"yyyy-MM-dd");
      response.getWriter().print(str);
    }

}
